﻿$(document).ready(function () {

    //      "ID":0,"siteID":1,"logo":"~\\logos\\hotnews_logo.gif","category":"Economie/Burse",
    //      "title":" Wall Street : noi recorduri pentru Dow Jones si S P 500","pubdate":"\/Date(1384402080000)\/",
    //      "description":""Cele mai importante stiri de azi au venit din rezultatele lui Macy, rezultatele grupului s-au dovedit mult mai bune decat era de asteptat", a declarat Hugh Johnson,  presedinte al Hugh Johnson Advisors, citat de AFP. Macy, marele lant american care vizeaza toate categoriile de venituri, serveste ca un barometru de consum intr-o tara a carei crestere se bazeaza in mare parte pe acest pilon. Si sezonul de Craciun este cel mai important pentru comercianti .","fullArticle":"This is a test of full article... needs implementation !!!!",
    //      "weblink":"http://economie.hotnews.ro/stiri-burse-16023137-wall-street-noi-recorduri-pentru-dow-jones-500.htm"}
    $("#AjaxTest1").click(function () {
        $.ajax({
            type: "POST",
            url: "WebForm2.aspx\GetArticle",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (jqXHR, textStatus, errorThrown) {
                // Boil the ASP.NET AJAX error down to JSON.
                var err = eval("(" + jqXHR.responseText + ")");

                // Display the specific error raised by the server (e.g. not a
                //   valid value for Int32, or attempted to divide by zero).
                alert(err.Message);
            },
            success: function (msg) {
                // Replace the div's content with the page method's return.
                alert(msg.d);
                $("#AjaxTest1").html(formatData(msg.d));
            }
        });
    });

    //
    //
    //
    //    $("#AjaxTest2").click(function () {
    //        $.ajax({
    //            type: "POST",
    //            url: "WebForm2.aspx\GetArticles",
    //            data: "{}",
    //            contentType: "application/json; charset=utf-8",
    //            dataType: "json",
    //            error: function (jqXHR, textStatus, errorThrown) {
    //                // Boil the ASP.NET AJAX error down to JSON.
    //                var err = eval("(" + xhr.responseText + ")");

    //                // Display the specific error raised by the server (e.g. not a
    //                //   valid value for Int32, or attempted to divide by zero).
    //                alert(err.Message);
    //            },
    //            success: function (data) {
    //                alert("Succes !  " + data.d);
    //                $("#AjaxTest2").html(data.d);
    //            }

    //            //                        formatData(data.d);

    //            //                    success: function (data) {
    //            //                        //                        $.each(data.d, function (index, element) {
    //            //                        //                            alert("succes !  " +index+ element);
    //            //                        //                            $('#result').append($('<div>', {
    //            //                        //                                text: element.name
    //            //                        //                            }));
    //            //                        //                        });
    //            //                        formatListofData(data.d);
    //            //                    }
    //        });
    //    });
});



function formatData(data) {
    var pdata = $.parseJSON(data);
    var table = '<table><tbody>';

    var row = '';
    row += '<tr><td> LOGO </td> <td>' + pdata.logo + '</td></tr>';
    row += '<tr><td> TITLE </td> <td>' + pdata.title + '</td></tr>';
    row += '<tr><td> CATEGORY </td> <td>' + pdata.category + '</td></tr>';
    row += '<tr><td> DESCRIPTION </td> <td>' + pdata.description + '</td></tr>';
    row += '<tr><td> WEBLINK </td> <td>' + pdata.weblink + '</td></tr>';
    table += row + '</tbody></table>';
    return table;
}

function formatListofData(msg) {
    var table = '<table><thead><tr><th>Date</th><th>Title</th><th>Excerpt</th></thead><tbody>';

    for (var post in msg) {
        var row = '<tr>';

        row += '<td>' + msg[post].Date + '</td>';
        row += '<td><a href="' + msg[post].Link + '">' + msg[post].Title + '</a></td>';
        row += '<td>' + msg[post].Description + '</td>';

        row += '</tr>';

        table += row;
    }

    table += '</tbody></table>';

    $('#Result').html(table);
}